package com.lbn.jfqsc.app.account.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lbn.baseframe.base.BaseActivity;
import com.lbn.baseframe.base.BaseRecyclerAdapter;
import com.lbn.baseframe.ui.NAppbar;
import com.lbn.jfqsc.R;
import com.lbn.jfqsc.app.account.order.adapter.OrderAdapter;
import com.lbn.jfqsc.app.account.order.adapter.SupportAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderActivity extends BaseActivity<OrderPresenter> implements IOrderView,
        XRecyclerView.LoadingListener, BaseRecyclerAdapter.OnItemClickListener {

    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    private BaseRecyclerAdapter adapter;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        ButterKnife.bind(this);
        setAppbar(new NAppbar(this));

        appbar.openBack();
        type = getIntent().getStringExtra("Type");
    }

    @Override
    public OrderPresenter createPresenter() {
        return new OrderPresenter();
    }

    @Override
    public void onPresenterCreated() {
        super.onPresenterCreated();

        if (type.equals("Support")) {
            appbar.setTitle("我的赞助");
            adapter = new SupportAdapter(this, null);
        } else {
            appbar.setTitle("我的订单");
            adapter = new OrderAdapter(this, null);
        }
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingListener(this);
        recyclerView.setRefreshing(true);
    }


    @Override
    public void onItemClick(View itemView, int pos) {
    }

    int page = 1;

    @Override
    public void onRefresh() {
        page = 1;
        if (type.equals("Support")) {
            mPresenter.supportList(page);
        } else {
            mPresenter.orderList(page);
        }
    }

    @Override
    public void onLoadMore() {
        page++;
        if (type.equals("Support")) {
            mPresenter.supportList(page);
        } else {
            mPresenter.orderList(page);
        }
    }

    @Override
    public void isMore(boolean b) {
        recyclerView.setNoMore(!b);
    }

    @Override
    public void setData(List list) {
        adapter.setData(list);
        recyclerView.refreshComplete();
    }

    @Override
    public void addData(List list) {
        adapter.setData(list);
        recyclerView.refreshComplete();
    }

    public static void startActivity(Context context, String type) {
        Intent intent = new Intent(context, OrderActivity.class);
        intent.putExtra("Type", type);
        context.startActivity(intent);
    }
}
